/*	kd:
	
	We have to tell the user SOMETHING and we do so with sprites. These have
	properties allowing them to slide on and off screen, but also do custom
	animations like bouncing and flashing.
	
	In reality, this just handles movement. You have to throw the sprites in
	yourself.
	
	*/

struct Gc_UiSprites {
	private Array <TextureId>		sprite;
	
	void ClearAndResize (uint size) {
		sprite.clear();
		sprite.resize(size);
	}
	
	TextureId At (int i) const {
		return sprite [i];
	}
	
	void Load (int index, String sprite_name) {
		sprite [index] = Gc_Sprite.Load(sprite_name);
	}
	
	vector2 Size (int index) const {
		return TexMan.GetScaledSize(sprite [index]);
	}
}

struct Gc_UiDirAnim {
	
	// kd: Definitions (including the offset vector cuz why not).
	Array <int>						def_tics;
	Array <double>					def_alpha;
	Array <double>					def_off_x;
	Array <double>					def_off_y;
	
	// kd: These positions get actually read.
	Array <double>					off_x;
	Array <double>					off_y;
	
	void ClearAndResize (uint size) {
		def_off_x.				clear();
		def_off_y.				clear();
		def_tics.				clear();
		def_alpha.				clear();
		off_x.					clear();
		off_y.					clear();
		
		def_off_x.				resize(size);
		def_off_y.				resize(size);
		def_tics.				resize(size);
		def_alpha.				resize(size);
		off_x.					resize(size);
		off_y.					resize(size);
	}
	
	void Set (
	int		index,
	int		tics		= 0,
	double	alpha		= 0,
	vector2	direction	= (0, 0)) {
		def_off_x	[index] = direction.x;
		def_off_y	[index] = direction.y;
		def_tics	[index] = max(0, tics);
		def_alpha	[index] = alpha;
	}
	
	void ScaleBySprite (
	int			index,
	TextureId	sprite) {
		let size = TexMan.GetScaledSize(sprite);
		off_x	[index] = def_off_x [index] * size.x;
		off_y	[index] = def_off_y [index] * size.y;
	}
}

struct Gc_UiAnim {
	
	// kd: Base data for positioning
	private Array <double>			def_pos_x;
	private Array <double>			def_pos_y;
	private Array <double>			def_diff_x;
	private Array <double>			def_diff_y;
	private Array <bool>			def_is_normal_coord;
	private int						count;
	
	// kd: Data for drawing - not private on purpose.
	Array <double>					pos_x;
	Array <double>					pos_y;
	Array <double>					diff_x;
	Array <double>					diff_y;
	
	void ClearAndResize (uint size) {
		def_pos_x.				clear();
		def_pos_y.				clear();
		def_diff_x.				clear();
		def_diff_y.				clear();
		def_is_normal_coord.	clear();
		pos_x.					clear();
		pos_y.					clear();
		diff_x.					clear();
		diff_y.					clear();
		
		def_pos_x.				resize(size);
		def_pos_y.				resize(size);
		def_diff_x.				resize(size);
		def_diff_y.				resize(size);
		def_is_normal_coord.	resize(size);
		pos_x.					resize(size);
		pos_y.					resize(size);
		diff_x.					resize(size);
		diff_y.					resize(size);
		
		count = size;
	}
	
	void Copy (int source, int dest) {
		def_pos_x			[dest] = def_pos_x				[source];
		def_pos_y			[dest] = def_pos_y				[source];
		def_diff_x			[dest] = def_diff_x				[source];
		def_diff_y			[dest] = def_diff_y				[source];
		def_is_normal_coord	[dest] = def_is_normal_coord	[source];
		pos_x				[dest] = pos_x					[source];
		pos_y				[dest] = pos_y					[source];
		diff_x				[dest] = diff_x					[source];
		diff_y				[dest] = diff_y					[source];
	}
	
	void Set (
	int		index,
	vector2	start_pos,
	vector2	end_pos,
	bool	is_normal_coord = true ) {
		def_pos_x			[index] = start_pos.x;
		def_pos_y			[index] = start_pos.y;
		def_diff_x			[index] = end_pos.x - start_pos.x;
		def_diff_y			[index] = end_pos.y - start_pos.y;
		def_is_normal_coord	[index] = is_normal_coord;
	}
	
	void OnInit (vector2 resolution = (1, 1)) {
		pos_x.copy(def_pos_x);
		pos_y.copy(def_pos_y);
		diff_x.copy(def_diff_x);
		diff_y.copy(def_diff_y);
		
		for(let i = 0; i < count; i++)
		if(def_is_normal_coord [i]) {
			pos_x		[i] *= resolution.x;
			pos_y		[i] *= resolution.y;
			diff_x		[i] *= resolution.x;
			diff_y		[i] *= resolution.y;
			
			// pos_x		[i] += diff_x [i];
			// pos_y		[i] += diff_y [i];
		}
	}
	
	void TranslateBySprite (
	int			index,
	TextureId	sprite,
	vector2		offset) {
		let size = TexMan.GetScaledSize(sprite);
		
		size.x *= offset.x;
		size.y *= offset.y;
		
		pos_x		[index] += size.x;
		pos_y		[index] += size.y;
		// diff_x		[index] += size.x;
		// diff_y		[index] += size.y;
	}
	
	void TranslateAbs (
	int			index,
	vector2		offset) {
		pos_x	[index] += offset.x;
		pos_y	[index] += offset.y;
	}
	
	vector2 FinalPos (int index) const {
		return (
			pos_x [index] + diff_x [index],
			pos_y [index] + diff_y [index]);
	}
}
